package com.example.mall.dao;

import com.example.mall.model.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

@Repository
public class AddressDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insertInfo(Address address) {
        String sql = "insert into address (name, phoneNumber, location, pID) values (?, ?, ?, ?)";
        jdbcTemplate.update(sql, address.getName(), address.getPhonenumber(), address.getLocation(), address.getpID());
    }

    public void delete(Integer id) {
        String sql = "delete from address where id = ?";
        jdbcTemplate.update(sql, id);
    }

    public void update(Address address) {
        String sql = "update address set name = ? , phoneNumber = ?, location = ? where id = ?";
        jdbcTemplate.update(sql, address.getName(), address.getPhonenumber(), address.getLocation(), address.getId());
    }


    public Address getById(Integer id) {
        String sql = "select * from address where id = ? lock in share mode";
        RowMapper<Address> rowMapper = new BeanPropertyRowMapper<>(Address.class);
        return jdbcTemplate.queryForObject(sql, rowMapper, id);
    }

}
